package com.xzk.config;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Classname FileDfsUtil
 * @Description TODO
 * @Date 2020/10/20 11:24
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
@Component
public class FileDfsUtil {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    /**
     * 上传文件
     * @param multipartFile
     * @return
     * @throws IOException
     */
    public String upload(MultipartFile multipartFile) throws IOException {
        String extName = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
        StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), extName, null);
        return storePath.getFullPath(); //group1/M00/00/00/xxxx.jpg
    }

    /**
     * 删除文件
     * @param fileUrl
     */
    public void deleteFile(String fileUrl){
        StorePath storePath = StorePath.parseFromUrl(fileUrl);
        fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
    }

}
